const Router = require('koa-router') // koa路由块
const Swagger = require('@/api/swagger')
const exec = require('child_process').execSync
let router = new Router()
/** 统一api前缀 */
const api = '/api'
const User = require('@/api/user')
const ProjectType = require('@/api/projectes/type')
const Project = require('@/api/projectes')
const UserProject = require('@/api/user_project')
const Task = require('@/api/task')
const ScheduleTask = require('@/api/schedule')
const Share = require('@/api/share')
const Note = require('@/api/note')
const File = require('@/api/file')
/** 测试ping的通过 */
router.get(`${api}/`, require('@/api/user/main'))
/** 用户模块接口 */
router.post(`${api}/login`, require('@/api/user/login'))
router.get(`${api}/find_same_name`, require('@/api/user/findSameName'))
router.get(`${api}/getUserInfo`, User.getPersonInfo)
router.post(`${api}/updateUserInfo`, User.updateUserInfo)
router.post(`${api}/register`, require('@/api/user/register'))
router.get(`${api}/findUserByName`, User.findUser)


/** 项目模块接口 */
router.get(`${api}/project/getProjectInfo/:id`, Project.getProjectById)
router.post(`${api}/project/insertProject`, User.insertProject)
router.put(`${api}/project/updateProject/:id`, User.updateProject)
router.delete(`${api}/project/removeProjectType/:id`, ProjectType.removeProjectType)

router.delete(`${api}/project/removeProject/:id`, User.removeProject)
router.put(`${api}/project/returnProject/:id`, User.returnProject)
router.put(`${api}/project/updateProjectType/:id`, ProjectType.updateProjectType)

/** 用户项目模块 */
router.get(`${api}/project/getAllUserByProject`, UserProject.getAllTeamer)
router.post(`${api}/project/inviteUser`, UserProject.InviteUser)
router.delete(`${api}/project/removeUser/:id`, UserProject.removeUser)
router.put(`${api}/project/setAdmin/:id`, UserProject.setAdmin)

/** 项目类型接口 */
router.get(`${api}/project/getAllProjectType`, ProjectType.getAllProjectType)
router.post(`${api}/project/insertProjectType`, ProjectType.insertProjectType)
router.get(`${api}/project/getAllProject`, Project.getAllProject)

/** 任务模块接口 */
router.post(`${api}/project/insertTask/:id`, Task.insertTask)
router.post(`${api}/project/updateTaskStatus`, Task.updateTaskStatus)
router.get(`${api}/project/getAllTask/:id`, Task.getAllTask)
router.put(`${api}/project/updateTask/:id`, Task.updateTask)
router.delete(`${api}/project/removeTask/:id`, Task.removeTask)

/** 日程模块接口 */
router.post(`${api}/project/updateScheduleTaskStatus`, ScheduleTask.updateTaskStatus)
router.post(`${api}/project/insertScheduleTask/:id`, ScheduleTask.insertScheduleTask)
router.get(`${api}/project/getAllScheduleTask/:id`, ScheduleTask.getAllScheduleTask)
router.put(`${api}/project/updateScheduleTask/:id`, ScheduleTask.updateScheduleTask)
router.delete(`${api}/project/removeScheduleTask/:id`, ScheduleTask.removeScheduleTask)

/** 分享模块接口 */
router.get(`${api}/project/getAllShareInfo/:id`, Share.getAllShare)
router.post(`${api}/project/insertShareInfo/:id`, Share.insertShare)
router.put(`${api}/project/updateShareInfo/:id`, Share.updateShare)
router.delete(`${api}/project/removeShareInfo/:id`, Share.removeShare)

/** 留言模块接口 */
router.post(`${api}/project/insertNote/:id`, Note.insertNote)
router.get(`${api}/project/getAllNote/:id`, Note.getAllNote)
router.put(`${api}/project/updateNote/:id`, Note.updateNote)
router.delete(`${api}/project/removeNote/:id`, Note.removeNote)

/** 文件上传模块接口是u */
router.post(`${api}/upload/project/:id`, File.upLoadFile)
router.post(`${api}/upload/normal`, File.upLoadImage)
router.get(`${api}/file/project/:id`, File.getAllFile)
router.get(`${api}/file/download/:id`, File.dowmLoad)
router.delete(`${api}/file/project/:id`, File.removeFile)

// serve swagger
router.get('/swagger.json', async function (ctx) {
    ctx.set('Content-Type', 'application/json');
    ctx.body = Swagger();
});

router.post('/webhooks', async (ctx) => {
    console.log('/webhooks', process.cwd())
    exec('sh config/pull.sh', (err, sto) => {
        console.log('监听主分支代码有变动，已经为你拉取最新代码，pm2启动时将会自动部署，否则将需要手动去更新项目,详细请看日志', sto)
    })
})

module.exports = router
